package com.example.demospringboot;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhaoPin
 * @Date 2024/7/23  16:36
 * @description: 多人会签
 */

@SpringBootTest
public class Flowable05MultiInstance {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    /**
     * 流程部署操作
     */
    @Test
    void deployFlow() throws InterruptedException {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/05-会签/multiInstance-demo6.bpmn20.xml")
                .name("多人会签-06")
                .deploy();// 部署的方法
        System.out.println("*******  deploy.getId() = " + deploy.getId());

    }

    /**
     * 启动流程实例
     *  vkaffbzroxqgbghd
     */
    @Test
    void startFlow() throws InterruptedException{
        // 在流程定义表中动态维护
        String processId = "multiInstance-demo5:1:6ee88633-4992-11ef-afc8-b469218e373e";
        Map<String,Object> map = new HashMap<>();
        // 1.根据流程定义ID启动流程实例
        map.put("users", Arrays.asList("张三24","李四24"));
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processId,map);
        //Thread.sleep(Integer.MAX_VALUE);

        // MyGlobalEventListener myGlobalEventListener = new MyGlobalEventListener();
        // runtimeService.addEventListener(myGlobalEventListener, FlowableEngineEventType.ACTIVITY_CANCELLED);
    }

    /**
     * 任务的审批
     */
    @Test
    void completeTask() throws InterruptedException {
        Map<String, Object> map = new HashMap<>();
        map.put("flag",true);
        taskService.complete("84aa9857-4992-11ef-be65-b469218e373e",map);
        //Thread.sleep(Integer.MAX_VALUE);
    }

}
